home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_07_01 / v7n1090a.txt < prev    next >
Text File  |  1988-10-08  |  4KB  |  163 lines

  1. /*
  2.  *  browse.c - Very simple file browser.
  3.  *
  4.  *  Syntax:
  5.  *      browse file1 file2 ...
  6.  */
  7.  
  8. #include <stdio.h>
  9. #ifdef  __ZTC__     /* if MS-DOS/Zortech C */
  10. #include <disp.h>
  11. #else
  12. #include <curses.h>
  13. #define disp_printf printw
  14. #define    disp_move   move
  15. #endif
  16.  
  17. #define MAX_LINE    (80)
  18. #define PAGE_SIZE   (20)
  19.  
  20.  
  21.     main(argc, argv)
  22. char    **argv;
  23. {
  24. int     i, c;
  25. FILE    *fin;
  26.  
  27. #ifdef  __ZTC__
  28.     disp_open();    /* Initialize display package */
  29.     disp_move(0,0);
  30.     disp_eeop();
  31.     disp_flush();
  32. #else
  33.     initscr();
  34.     noecho();
  35.     scrollok(stdscr, TRUE);
  36.     raw();          /* unbuffered character input  */
  37. #endif
  38.  
  39.     for(i = 1; i < argc; ++i)
  40.         if((fin=fopen(argv[i], "r")) == NULL)
  41.             {
  42.             c   = toupper(
  43.                 pause( "Can't open file for input. Continue (Y/n)? ", "yYnN" )
  44.                          );
  45.             if(c == 'N')
  46.                 break;
  47.             }
  48.         else if(!browse(fin))
  49.             break;
  50.  
  51. #ifdef  __ZTC__
  52.     disp_close();   /* wrap up display package  */
  53. #else
  54.     endwin();   /* set terminal to initial state    */
  55. #endif
  56. }
  57.  
  58. int     browse(fin)
  59. FILE    *fin;
  60. {
  61. char    line[MAX_LINE];
  62. int     c, nlines = 1, eof = 0, addlines = PAGE_SIZE;
  63.  
  64. #if __ZTC__
  65.     disp_move(0, 0);
  66.     disp_eeop();
  67.     disp_move(1, 0);
  68. #else
  69.     clear();
  70.     move(1, 0);    
  71. #endif
  72.     for(;;)
  73.         {
  74.         if(addlines)    /* wants to see more    */
  75.             if(eof)
  76.                 putchar( 7 );   /* beep */
  77.             else
  78.                 {
  79.                 while(fgets(line, MAX_LINE, fin))
  80.                     {
  81.                     if(nlines > PAGE_SIZE)
  82. #ifdef __ZTC__
  83.                         disp_scroll(1,0,0,PAGE_SIZE,79,DISP_NORMAL);
  84. #else
  85.                         scroll(stdscr);
  86. #endif
  87.                     else
  88.                         ++nlines;
  89.                     disp_move(nlines-1, 0);
  90.                     disp_printf("%s", line);
  91.                     if(--addlines <= 0)
  92.                         break;
  93.                     }
  94.                 if(addlines)    /* hit end of file  */
  95.                     {
  96.                     eof = 1, addlines = 0;
  97. #if __ZTC__
  98.                     disp_setattr(DISP_REVERSEVIDEO);
  99.                     disp_printf( "End-of-File" );
  100.                     disp_setattr(DISP_NORMAL);
  101. #else
  102.                     standout(); printw( "End-of-File" ); standend();
  103. #endif
  104.                     }
  105.                 }
  106.  
  107.         c   = pause("BROWSE: next File, Page, Line, or Quit (F,P,L,Q)? ",
  108.                     "fFpPlLqQ");
  109.         c   = toupper(c);
  110.         if(c == 'F')
  111.             {
  112.             fclose(fin);
  113.             return( 1 );
  114.             }
  115.         else if(c == 'P')
  116.             addlines    = PAGE_SIZE;
  117.         else if(c == 'L')
  118.             addlines    = 1;
  119.         else if(c == 'Q')
  120.             {
  121.             fclose(fin);
  122.             return( 0 );
  123.             }
  124.         else
  125.             putchar( 7 );   /* beep */
  126.         }
  127. }
  128.  
  129. int     pause(prompt, chars)
  130. char    *prompt, *chars;
  131. {
  132. #if __ZTC__
  133. extern  int disp_cursorrow, disp_cursorcol;
  134. #endif
  135. int     c, row, col;
  136.  
  137. #if __ZTC__
  138.     row = disp_cursorrow, col = disp_cursorcol;
  139. #endif
  140.  
  141. #if __ZTC__
  142.     disp_move(0, 0);
  143.     disp_setattr(DISP_REVERSEVIDEO);
  144.     disp_printf( "%s", prompt );
  145.     disp_flush();
  146. #else
  147.     getyx(stdscr, row, col);
  148.     move(0, 0); standout();
  149.     printw( "%s", prompt );
  150.     refresh();
  151. #endif
  152.     while( !strchr(chars, (c=getch())) )
  153.         putchar(7); /* beep */
  154. #if __ZTC__
  155.     disp_setattr(DISP_NORMAL);
  156.     disp_move(row, col);
  157. #else
  158.     standend();
  159.     move(row, col);
  160. #endif
  161.     return( c );
  162. }
  163.